package com.kexuexiong.demo.bear.elasticsearch.service.impl;

import com.kexuexiong.demo.bear.elasticsearch.dto.ESSearchDTO;
import com.kexuexiong.demo.bear.elasticsearch.model.Sougoulog;
import com.kexuexiong.demo.bear.elasticsearch.service.EsBaseService;
import com.kexuexiong.demo.bear.elasticsearch.service.EsSougouService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("esSougouServiceImpl")
@Slf4j
public class EsSougouServiceImpl extends EsBaseService implements EsSougouService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @Override
    public SearchHits<Sougoulog> search(ESSearchDTO esSearchDTO) {
        // 页码
        if (esSearchDTO.getPageNumber() == null || esSearchDTO.getPageNumber() < 0) {
            esSearchDTO.setPageNumber(0);
        }
        // 页数
        if (esSearchDTO.getPageSize() == null || esSearchDTO.getPageSize() < 1) {
            esSearchDTO.setPageSize(10);
        }
        SearchHits<Sougoulog> personSearchHits = null;
        try {
            // 构建查询
            NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
            // 组合查询，boost即为权重，数值越大，权重越大
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .should(QueryBuilders.multiMatchQuery(esSearchDTO.getKeyword(), "url").boost(3))
                    .should(QueryBuilders.multiMatchQuery(esSearchDTO.getKeyword(), "keywords").boost(2));
            searchQuery.withQuery(queryBuilder);
            // 高亮设置
            List<String> highlightFields = new ArrayList<String>();
            highlightFields.add("url");
            highlightFields.add("keywords");
            HighlightBuilder.Field[] fields = new HighlightBuilder.Field[highlightFields.size()];
            for (int x = 0; x < highlightFields.size(); x++) {
                fields[x] = new HighlightBuilder.Field(highlightFields.get(x)).preTags("<a>")
                        .postTags("</a>");
            }
            searchQuery.withHighlightFields(fields);
            // 分页设置
            searchQuery.withPageable(PageRequest.of(esSearchDTO.getPageNumber(), esSearchDTO.getPageSize()));
            personSearchHits = elasticsearchRestTemplate.search(searchQuery.build(), Sougoulog.class);
            return personSearchHits;
        } catch (Exception e) {
            log.error("es查询出错：{}", e.getMessage());
        }
        return personSearchHits;
    }
}
